layui.use(['form', 'layer', 'layedit', 'laydate', 'upload'], function() {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    //用于同步编辑器内容到textarea
    layedit.sync(editIndex);




    //上传缩略图
    // upload.render({
    //     elem: '.thumbBox',
    //     url: '../../json/userface.json',
    //     method: "get", //此处是为了演示之用，实际使用中请将此删除，默认用post方式提交
    //     done: function(res, index, upload) {
    //         var num = parseInt(4 * Math.random()); //生成0-4的随机数，随机显示一个头像信息
    //         $('.thumbImg').attr('src', res.data[num].src);
    //         $('.thumbBox').css("background", "#fff");
    //     }
    // });
    // 招聘信息
    $.get(severUrl + "get_recruit_all_data", function(result) {
        console.log("00000");
        console.log(result)
        var datas = result.data;
        var options = '';

        for (var i = 0; i < datas.length; i++) {
            var zhaopinTitle = datas[i].title;
            var recruitId = datas[i].recruit_id;
            console.log(zhaopinTitle)
            options += '<option value="' + recruitId + '">' + zhaopinTitle + '</option>';

        }
        $(".selects").append(options)
        form.render()
            // console.log($(".selects").text())

    });
    //格式化时间
    function filterTime(val) {
        if (val < 10) {
            return "0" + val;
        } else {
            return val;
        }
    }

    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear() + '-' + filterTime(time.getMonth() + 1) + '-' + filterTime(time.getDate()) + ' ' + filterTime(time.getHours()) + ':' + filterTime(time.getMinutes()) + ':' + filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger: "click",
        done: function(value, date, endDate) {
            submitTime = value;
        }
    });
    form.on("radio(release)", function(data) {
        if (data.elem.title == "定时发布") {
            $(".releaseDate").removeClass("layui-hide");
            $(".releaseDate #release").attr("lay-verify", "required");
        } else {
            $(".releaseDate").addClass("layui-hide");
            $(".releaseDate #release").removeAttr("lay-verify");
            submitTime = time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate() + ' ' + time.getHours() + ':' + time.getMinutes() + ':' + time.getSeconds();
        }
    });

    form.verify({
        newsName: function(val) {
            if (val == '') {
                return "文章标题不能为空";
            }
        },
        content: function(val) {
            if (val == '') {
                return "文章内容不能为空";
            }
        }
    })

    function imginput(index) {
        console.log("xxx")
    }
    // form.on("submit(formDemo)", function(data) {
    //     // layer.msg()


    //     console.log($('#inputImg').val())
    //     console.log("xxxxx")
    //     console.log(JSON.stringify(data.field))
    //     console.log(data)
    //         //截取文章内容中的一部分文字放入文章摘要
    //         // var abstract = layedit.getText(editIndex).substring(0, 50);
    //         //弹出loading
    //     var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
    //     // 实际使用时的提交信息
    //     $.post(severUrl + "insert_lunbo_data", {
    //         title: data.field.title, //文章标题
    //         recruit_id: data.field.works, //文章摘要
    //     }, function(res) {
    //         console.log("88888888888")
    //         console.log(res)
    //     })
    //     setTimeout(function() {
    //         console.log($("#inputImg").val())
    //         top.layer.close(index);

    //         top.layer.msg("轮播图添加成功！");
    //         layer.closeAll("iframe");
    //         //刷新父页面
    //         parent.location.reload();
    //     }, 500);
    //     return false;
    // })

    //预览
    form.on("submit(look)", function() {
        layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问");
        return false;
    })

    //创建一个编辑器
    var editIndex = layedit.build('news_content', {
        height: 535,
        uploadImage: {
            url: "../../json/newsImg.json"
        }
    });

})